package com.xinqizhi.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinqizhi.entity.SysMenuEntity;
import com.xinqizhi.pojo.SysMenuPojo;
import com.xinqizhi.vo.SysMenuVo;
import com.xinqizhi.service.SysMenuService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author Rabit
 * @email lj79490324@gmail.com
 * @date 2019-05-09 00:02:09
 */
@Controller
@RequestMapping("/sys")
public class SysMenuController {
    private final static Logger log = Logger.getLogger(SysMenuController.class);
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 跳转到后台系统主页，并显示右侧菜单
     */
    @RequestMapping("/index")
    public String sysWorkIndex(HttpSession session){
        /**
         * 查询管理界面右侧一级菜单
         */
        List<SysMenuVo> menuVoList= sysMenuService.selecRightMenuByPid();
        session.setAttribute("menulist",menuVoList);

        return "main";
    }


    /**
     * 右侧二级菜单点击时，菜单列表页面跳转，@RequestMapping的值和数据库中的值对应
     * @return
     */
    @RequestMapping("/menulistjsp")
    public String toMenuListJsp(){
        return "redirect:../views/menu-list.jsp";
    }

    /**
     * 对菜单管理查询列表
     * result.put("allmenu",allmenu);
     * @return
     */
    @RequestMapping("/getmenulist")
    @ResponseBody
    public Map getMenuList(SysMenuEntity entity){
        log.info("分页传参+++="+entity.toString()+entity.getPaging());
        Map<String, Object> result = new HashMap<>();
        if (entity.getPaging()){
            PageHelper.startPage(entity.getPage(),entity.getLimit());
        }
        List<SysMenuEntity> list = sysMenuService.selectMenuList();
        PageInfo page = new PageInfo(list);
        result.put("data",page.getList());
        result.put("count",page.getTotal());
        result.put("succes",true);
        result.put("code",0);
        result.put("msg","查询所有菜单成功");
        return result;
    }

    /**
     * 在menu-edit.jsp中，页面加载时获取要修改的menu信息
     * @param menuid
     * @return
     */
    @RequestMapping("/getMenuById/{menuid}")
    @ResponseBody
    public Map getMenuById(@PathVariable("menuid")Integer menuid){
        log.info("通过menuid查询修改"+menuid);
        Map<String, Object> result = sysMenuService.selectMneuById(menuid);
        result.put("success",true);
        return result;
    }

    /**
     * 修改菜单可用状态
     * @param pojo
     * @return
     */
    @RequestMapping("/updatemenu")
    @ResponseBody
    public Map updateMenu(SysMenuPojo pojo){
        log.info("修改--------------------------------"+pojo.toString());
        Map result = sysMenuService.updateMenuById(pojo);
        return result;
    }



}
